home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Extensions / Imaging / PIL / GimpPaletteFile.py < prev    next >
Encoding:
Python Source  |  2000-06-23  |  1.2 KB  |  58 lines

  1. #
  2. # Python Imaging Library
  3. # $Id: GimpPaletteFile.py,v 1.1.1.1 1998/08/18 13:07:54 sjoerd Exp $
  4. #
  5. # stuff to read GIMP palette files
  6. #
  7. # History:
  8. #    97-08-23 fl    Created
  9. #
  10. # Copyright (c) Secret Labs AB 1997.
  11. # Copyright (c) Fredrik Lundh 1997.
  12. #
  13. # See the README file for information on usage and redistribution.
  14. #
  15.  
  16. import string
  17.  
  18. class GimpPaletteFile:
  19.  
  20.     rawmode = "RGB"
  21.  
  22.     def __init__(self, fp):
  23.  
  24.         self.palette = map(lambda i: chr(i)*3, range(256))
  25.  
  26.     if fp.readline()[:12] != "GIMP Palette":
  27.         raise SyntaxError, "not a GIMP palette file"
  28.  
  29.         i = 0
  30.  
  31.         while i <= 255:
  32.  
  33.             s = fp.readline()
  34.  
  35.             if not s:
  36.                 break
  37.             if len(s) > 100:
  38.                 raise SyntaxError, "bad palette file"
  39.  
  40.             if s[0] == "#":
  41.                 continue
  42.  
  43.             v = tuple(map(string.atoi, string.split(s)[:3]))
  44.             if len(v) != 3:
  45.                 raise ValueError, "bad palette entry"
  46.  
  47.             if 0 <= i <= 255:
  48.                 self.palette[i] = chr(v[0]) + chr(v[1]) + chr(v[2])
  49.  
  50.             i = i + 1
  51.  
  52.         self.palette = string.join(self.palette, "")
  53.  
  54.  
  55.     def getpalette(self):
  56.  
  57.         return self.palette, self.rawmode
  58.